package com.kongmou.hsearch.section1;

import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.util.List;

@Component
@Transactional
public class InitFullTextIndex {


    @Resource
    AppRepository appRepository;
    //@PersistenceContext
    @Resource
    private EntityManagerFactory emf;

    @Autowired
    public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
        this.emf = emf;
    }

    // @Autowired
    //private final EntityManager em;


    // @Autowired
    // public InitFullTextIndex(EntityManager emf) {
    //    super();
    //   this.em = emf;
    // }


    public void initFullTextIndex() throws Exception {
        EntityManager em = emf.createEntityManager();
        App app = appRepository.getOne(Long.valueOf(4444));
        App app2 = appRepository.getAppById(Long.valueOf(4444));
        //App app3 = appRepository.findByNameLike("name");
        List<App> lists = appRepository.findAll();

        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
        fullTextEntityManager.createIndexer().startAndWait();

    }


    @Transactional
    public void queryIndext() {
        EntityManager em = emf.createEntityManager();
        FullTextEntityManager fullTextEntityManager =
                org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
        em.getTransaction().begin();
// create native Lucene query using the query DSL
// alternatively you can write the Lucene query using the Lucene query parser
// or the Lucene programmatic API. The Hibernate Search DSL is recommended though
        QueryBuilder qb = fullTextEntityManager.getSearchFactory()
                .buildQueryBuilder().forEntity(App.class).get();
        org.apache.lucene.search.Query query = qb
                .keyword()
                .onFields("name", "description")
                .matching("name4")
                .createQuery();
// wrap Lucene query in a javax.persistence.Query
        javax.persistence.Query persistenceQuery =
                fullTextEntityManager.createFullTextQuery(query, App.class);
// execute search
        List result = persistenceQuery.getResultList();
        org.apache.lucene.search.Query query2 = qb
                .all()
                .createQuery();
        javax.persistence.Query persistenceQuery2 =
                fullTextEntityManager.createFullTextQuery(query2, App.class);
        persistenceQuery2.setFirstResult(1);
        persistenceQuery2.setMaxResults(2);
        List result2 = persistenceQuery2.getResultList();
        em.getTransaction().commit();
        em.close();
    }


}
